/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJPanel.java
 *
 * Created on Jan 8, 2012, 10:28:08 PM
 */
package GUI;

import Bussiness.ChungLoai;
import Bussiness.NhanVien;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Admin
 */
public class jpNhanVien extends javax.swing.JPanel {
    static String bot;
    private DefaultTableModel modelTable = new DefaultTableModel();
    /** Creates new form NewJPanel */
    public jpNhanVien() {
        initComponents();
        this.display();
        bot = "";
        //this.loadCombo();
        //this.display();
        }
    public void loadTable(Vector v)
    {
        modelTable.setNumRows(0);
        Vector v1 = new Vector();
        v1.addElement("Mã Nhân Viên");
        v1.addElement("Tên Nhân Viên");
        v1.addElement("Chức Vụ");
        v1.addElement("Giới Tính");
        
        modelTable.setColumnIdentifiers(v1);
        NhanVien b = new NhanVien();
        for (int i = 0; i < v.size(); i++) {
            b = (NhanVien)v.get(i);
            Vector row = new Vector();
            row.addElement(b.getMaNV());
            row.addElement(b.getTenNV());
            row.addElement(b.getChucVu());
            row.addElement(b.getGioiTinh());
            modelTable.addRow(row);
        }
        tblNhanVien.setModel(modelTable);
        int r = tblNhanVien.getSelectedRow();
        if(r < 0){
            this.bot = "";
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
            return;
        }
        else{
            bot = tblNhanVien.getValueAt(r, 0).toString();
        }
    }
    public void reset()
    {
        txtMaNV.setText("");
        txtTenNV.setText("");
        txtChucVu.setText("");
        cboGT.setSelectedItem(bot);
    }
    public void add()
    {
        NhanVien b = new NhanVien();
        b.setMaNV(txtMaNV.getText());
        b.setTenNV(txtTenNV.getText());
        b.setChucVu(txtChucVu.getText());
        b.setGioiTinh((String)cboGT.getSelectedItem());
       
        int t = 0;
        try{
            t = b.insertNhanVien(b);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"Add successful","Infofrm",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Add erro!");
       this.reset();
    }
    

    
    public void display()
    {
        NhanVien b1 = new NhanVien();
        Vector v1 = new Vector();
        try {
           v1=b1.getAllNhanVien();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.loadTable(v1);
    }
    public void search(){
        NhanVien b=new NhanVien();
        Vector v=new Vector();        
        try {
            if (cbSearch.getSelectedIndex()==0)
                v=b.getNhanVien_MaNV(txtSearch.getText());
            else
                 if (cbSearch.getSelectedIndex()==1)
                v=b.getNCC_TenNV(txtSearch.getText());
                
           } catch (SQLException e) {
            e.printStackTrace();
        }  
          this.loadTable(v);
    }
    public void update()
    {
        NhanVien u = new NhanVien();
        u.setMaNV(txtMaNV.getText());
        u.setTenNV(txtTenNV.getText());
        u.setChucVu(txtChucVu.getText());
        u.setGioiTinh(cboGT.getSelectedItem().toString());
        int t = 0;
        try{
            t = u.updateNhanVien(u);
        }catch(SQLException e){
            e.printStackTrace();
        }
        if(t==1)
            JOptionPane.showMessageDialog(this,"Update successful","Inform",JOptionPane.INFORMATION_MESSAGE);
        else
            JOptionPane.showMessageDialog(this,"Update error!");
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblNhanVien = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        txtMaNV = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtChucVu = new javax.swing.JTextField();
        cboGT = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        txtTenNV = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        cbSearch = new javax.swing.JComboBox();
        txtSearch = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        tblNhanVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblNhanVien.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblNhanVienMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblNhanVien);

        jLabel1.setText("Mã NV");

        jLabel3.setText("Chức Vụ");

        cboGT.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nu" }));

        jLabel4.setText("GT");

        jLabel2.setText("Tên NV");

        btnEdit.setText("Thêm");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        jButton2.setText("Sửa");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        btnDelete.setText("Xóa");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        jButton4.setText("Load");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        cbSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MaNV", "TenNV" }));

        btnSearch.setText("Tìm Kiếm");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        jLabel7.setText("Tìm theo: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtTenNV)
                    .addComponent(txtMaNV, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtChucVu, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboGT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(231, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(377, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnSearch))
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43))
            .addGroup(layout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(154, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtChucVu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(cboGT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtTenNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit)
                    .addComponent(jButton2)
                    .addComponent(btnDelete)
                    .addComponent(jButton4))
                .addGap(26, 26, 26)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addComponent(cbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSearch)
                .addContainerGap(29, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
// TODO add your handling code here:
    this.add();
    this.display();
}//GEN-LAST:event_btnEditActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
    this.update();
    this.display();
}//GEN-LAST:event_jButton2ActionPerformed

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
// TODO add your handling code here:
    if(!bot.equals("")){
        int show=JOptionPane.showConfirmDialog(this,"Are You sure delete ?","Inform",JOptionPane.YES_NO_OPTION);
        int t=0,count=0;
        NhanVien b=new NhanVien();
        if(show==JOptionPane.YES_OPTION){
            int [] rArr=tblNhanVien.getSelectedRows();
                try 
                {
                    for(int i=0;i<rArr.length;i++)
                    {
                        t=b.DelNhanVien(tblNhanVien.getValueAt(rArr[i],0).toString());
                        if(t==1)
                            count++;
                    }
                    if(count>0)
                    {
                        JOptionPane.showMessageDialog(this,"Deleted "+count+" record ","successful.",JOptionPane.INFORMATION_MESSAGE);
                        this.display();
                    }
                    else
                    {
                      JOptionPane.showMessageDialog(this,"Error","Inform",JOptionPane.ERROR_MESSAGE);  
                    }
                } catch (SQLException x) {               
                   System.out.println(x);
                }
        }
    }
    this.reset();
}//GEN-LAST:event_btnDeleteActionPerformed

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
 // TODO add your handling code here:
    this.display();
}//GEN-LAST:event_jButton4ActionPerformed

private void tblNhanVienMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblNhanVienMouseClicked
 // TODO add your handling code here:
    int r = tblNhanVien.getSelectedRow();
    if(r < 0){
        bot = "";
        return;
    }else{
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        bot = tblNhanVien.getValueAt(r, 0).toString();
        
        if (r != -1){
            txtMaNV.setText(String.valueOf(tblNhanVien.getValueAt(r, 0)));
            txtTenNV.setText(String.valueOf(tblNhanVien.getValueAt(r, 1)));
            txtChucVu.setText(String.valueOf(tblNhanVien.getValueAt(r, 2)));
            cboGT.setSelectedItem(String.valueOf(tblNhanVien.getValueAt(r, 3)));
             }
    }
}//GEN-LAST:event_tblNhanVienMouseClicked

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
 // TODO add your handling code here:
    this.search();
}//GEN-LAST:event_btnSearchActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSearch;
    private javax.swing.JComboBox cbSearch;
    private javax.swing.JComboBox cboGT;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblNhanVien;
    private javax.swing.JTextField txtChucVu;
    private javax.swing.JTextField txtMaNV;
    private javax.swing.JTextField txtSearch;
    private javax.swing.JTextField txtTenNV;
    // End of variables declaration//GEN-END:variables
}
